﻿/*
 * File: SquadSelectionScene.cs
 * Author: Gourav Das
 * Purpose: Scene for Selection of playing 12 each team
 * Created: May 02 2011
 */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.Xml;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    class Playing12SelectionScene : BaseMenuScene
    {
        ContentManager content;
        float pauseAlpha;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Playing12SelectionScene(string title)
            : base(title)
        {
            // Create our menu entries.
            MenuItem ProceedMenuItem = new MenuItem("Proceed", 0, false, true);
            MenuItem BackMenuItem = new MenuItem("Back", 1, true);

            // Hook up menu event handlers.
            ProceedMenuItem.Selected += ProceedMenuItem_Selected;
            BackMenuItem.Selected += BackMenuItem_Selected;

            // Add Items to the menu.
            MenuItems.Add(ProceedMenuItem);
            MenuItems.Add(BackMenuItem);

            TransitioningTime = TimeSpan.FromSeconds(0.5);
            TransitionedTime = TimeSpan.FromSeconds(0.5);
        }

        /// <summary>
        /// Event handler for when the ProceedMenuItem_Selected is selected.
        /// </summary>
        void ProceedMenuItem_Selected(object sender, PlayerIndexEventArgs e)
        {
            ExitScene();
            sceneEngine.AddScene(new TossScene("Toss"), ControllingPlayer);
        }


        /// <summary>
        /// Event handler for when the Back Menu is selected.
        /// </summary>
        void BackMenuItem_Selected(object sender, PlayerIndexEventArgs e)
        {
            ExitScene();
            sceneEngine.AddScene(new MatchSettingsScene("Match Settings",Game22Yards.match.Team1, Game22Yards.match.Team2), ControllingPlayer);
        }

        /// <summary>
        /// Load graphics and non-graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(sceneEngine.Game.Services, "22yardsContent");

            Game22Yards.matchPlayEngine.match.Team1.players.Clear();
            Game22Yards.matchPlayEngine.match.Team2.players.Clear();

            //slow Processes
            Team.LoadIntlPlayersFromXML(ref Game22Yards.matchPlayEngine.match.Team1, 
                Game22Yards.matchPlayEngine.match.Team1.getRegionIDFromTeamID(), 
                Game22Yards.matchPlayEngine.match.Team1.getCountryIDFromTeamID());

            Team.LoadIntlPlayersFromXML(ref Game22Yards.matchPlayEngine.match.Team2,
                Game22Yards.matchPlayEngine.match.Team2.getRegionIDFromTeamID(),
                Game22Yards.matchPlayEngine.match.Team2.getCountryIDFromTeamID());

            List<cPlayer> tmpPlayersList1 = new List<cPlayer>();
            List<cPlayer> tmpPlayersList2 = new List<cPlayer>();

            //Dummy to select 12 players and trim the List Players to 12 selected players only
            for (int i = 0; i < 12; i++)
            {
                Game22Yards.matchPlayEngine.match.Team1.players[i].InPlaying11 = true;
                Game22Yards.matchPlayEngine.match.Team2.players[i].InPlaying11 = true;

                if (i == 3)
                {
                    Game22Yards.matchPlayEngine.match.Team1.players[i].IsCaptain = true;
                    Game22Yards.matchPlayEngine.match.Team2.players[i].IsWicketKeeper = true;
                }

                if (i == 6)
                {
                    Game22Yards.matchPlayEngine.match.Team1.players[i].Is12thMan = true;
                    Game22Yards.matchPlayEngine.match.Team2.players[i].IsCaptain = true;
                }

                if (i == 8)
                {
                    Game22Yards.matchPlayEngine.match.Team1.players[i].IsWicketKeeper = true;
                    Game22Yards.matchPlayEngine.match.Team2.players[i].Is12thMan = true;
                }

                tmpPlayersList1.Add(Game22Yards.matchPlayEngine.match.Team1.players[i]);
                tmpPlayersList2.Add(Game22Yards.matchPlayEngine.match.Team2.players[i]);
            }

            Game22Yards.matchPlayEngine.match.Team1.players.Clear();
            Game22Yards.matchPlayEngine.match.Team2.players.Clear();

            Game22Yards.matchPlayEngine.match.Team1.players = tmpPlayersList1;
            Game22Yards.matchPlayEngine.match.Team2.players = tmpPlayersList2;

            // If loading is slow, uncomment below line and use LoadingScene in previous screen to load this screen
            sceneEngine.Game.ResetElapsedTime();

        }


        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }

        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you switch to a different application.
        /// </summary>
        public override void Update(GameTime _gameTime, bool _otherSceneHasFocus,
                                                       bool _coveredByOtherScene)
        {
            base.Update(_gameTime, _otherSceneHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (_coveredByOtherScene)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                //base.Update(_gameTime, _otherScreenHasFocus, _coveredByOtherScreen);

                // Update each nested MenuEntry object.
                for (int i = 0; i < MenuItems.Count; i++)
                {
                    bool isSelected = IsActive && (i == SelectedItem);

                    MenuItems[i].Update(this, isSelected, _gameTime);
                }
            }

        }


        /// <summary>
        /// Draws the Scene with the menus.
        /// </summary>
        public override void Draw(GameTime _gameTime)
        {
            UpdateMenuItemLocations();

            sceneEngine.SpriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < MenuItems.Count; i++)
            {
                MenuItem menuItem = MenuItems[i];

                bool isSelected = IsActive && (i == SelectedItem);

                menuItem.Draw(this, isSelected, _gameTime);
            }

            // Make the menu title slide into place during transitions, this makes
            // the movement slow down as it nears the end
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(sceneEngine.GraphicsDevice.Viewport.Width / 2, ((float)80 * Game22Yards.scaleFactor.htScale));
            Vector2 titleOrigin = sceneEngine.TxtNFont.spFontGoodTimes.MeasureString(MenuTitle) / 2;
            Vector2 lftplayerPos = new Vector2(((float)300 * Game22Yards.scaleFactor.widthScale), ((float)200 * Game22Yards.scaleFactor.htScale));
            Vector2 rtplayerPos = new Vector2(((float)700 * Game22Yards.scaleFactor.widthScale), ((float)200 * Game22Yards.scaleFactor.htScale));

            titlePosition.Y -= transitionOffset * 100;
            //leftpos.Y -= transitionOffset * 500;
            //rightpos.Y -= transitionOffset * 500;
            lftplayerPos.X -= transitionOffset * 500;
            rtplayerPos.X += transitionOffset * 500;
            
            //Draw Main menu title
            sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontGoodTimes, MenuTitle, titlePosition, Color.White, 0,
                                   titleOrigin, 1.5f, SpriteEffects.None, 0);

            foreach (cPlayer cpl in Game22Yards.matchPlayEngine.match.Team1.players)
            {
                if (cpl.Is12thMan)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(12)" + cpl.Name, lftplayerPos, Color.White);
                else if (cpl.IsWicketKeeper)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(WK)" + cpl.Name, lftplayerPos, Color.White);
                else if (cpl.IsCaptain)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(C)" + cpl.Name, lftplayerPos, Color.White);
                else
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, cpl.Name, lftplayerPos, Color.White);

                lftplayerPos.Y += ((float)25 * Game22Yards.scaleFactor.htScale);
            }
            foreach (cPlayer cpl in Game22Yards.matchPlayEngine.match.Team2.players)
            {
                if (cpl.Is12thMan)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(12)" + cpl.Name, rtplayerPos, Color.White);
                else if (cpl.IsWicketKeeper)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(WK)" + cpl.Name, rtplayerPos, Color.White);
                else if (cpl.IsCaptain)
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, "(C)" + cpl.Name, rtplayerPos, Color.White);
                else
                    sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, cpl.Name, rtplayerPos, Color.White);
                rtplayerPos.Y += ((float)25 * Game22Yards.scaleFactor.htScale);
            }

            sceneEngine.SpriteBatch.End();
        }


        /// <summary>
        /// When the user presses ESC, do nothing.
        /// </summary>
        protected override void OnCancel(PlayerIndex _playerIndex)
        {
            //Do nothing :)
        }
    }
}
